﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Deferred.Editor.Scene;
using Deferred.Library.Scene;
using Deferred.Library.Scene.Utilities;

namespace Deferred.Editor.Gui.Scene
{
    public partial class TerrainPanel : UserControl
    {
        private ContentChooserWindow chooserWindow;

        public TerrainPanel(Terrain t, ContentChooserWindow chooserWindow)
        {
            InitializeComponent();

            this.chooserWindow = chooserWindow;

            terrainWrapperBindingSource.DataSource = new TerrainWrapper(t);
            terrainBindingSource.DataSource = t;
            terrainGenerationDataBindingSource.DataSource = t.GenerationData;
            terrainTexturePackBindingSource.DataSource = t.TexturePack;
        }

        private void heightMapBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null) {
                ((Terrain)terrainBindingSource.DataSource).HeightmapName = newTexture;
                terrainBindingSource.ResetBindings(false);
            }
        }

        private void lowTextureBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null)
            {
                ((TerrainTexturePack)terrainTexturePackBindingSource.DataSource).LowTextureName = newTexture;
                terrainTexturePackBindingSource.ResetBindings(false);
            }
        }

        private void lowNormalBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null)
            {
                ((TerrainTexturePack)terrainTexturePackBindingSource.DataSource).LowNormalName = newTexture;
                terrainTexturePackBindingSource.ResetBindings(false);
            }
        }

        private void midTextureBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null)
            {
                ((TerrainTexturePack)terrainTexturePackBindingSource.DataSource).MidTextureName = newTexture;
                terrainTexturePackBindingSource.ResetBindings(false);
            }
        }

        private void midNormalBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null)
            {
                ((TerrainTexturePack)terrainTexturePackBindingSource.DataSource).MidNormalName = newTexture;
                terrainTexturePackBindingSource.ResetBindings(false);
            }
        }

        private void highTextureBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null)
            {
                ((TerrainTexturePack)terrainTexturePackBindingSource.DataSource).HighTextureName = newTexture;
                terrainTexturePackBindingSource.ResetBindings(false);
            }
        }

        private void highNormalBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null)
            {
                ((TerrainTexturePack)terrainTexturePackBindingSource.DataSource).HighNormalName = newTexture;
                terrainTexturePackBindingSource.ResetBindings(false);
            }
        }

        private void slopeTextureBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null)
            {
                ((TerrainTexturePack)terrainTexturePackBindingSource.DataSource).SlopeTextureName = newTexture;
                terrainTexturePackBindingSource.ResetBindings(false);
            }
        }

        private void slopeNormalBrowseButton_Click(object sender, EventArgs e)
        {
            string newTexture = chooseTexture();
            if (newTexture != null)
            {
                ((TerrainTexturePack)terrainTexturePackBindingSource.DataSource).SlopeNormalName = newTexture;
                terrainTexturePackBindingSource.ResetBindings(false);
            }
        }

        private string chooseTexture()
        {
            chooserWindow.ValidExtensions = new List<string> { ".bmp", ".jpg", ".png", ".tga" };

            if (DialogResult.OK == chooserWindow.ShowDialog(this))
            {
                return chooserWindow.SelectedFileWithoutExtension;
            }

            return null;
        }

        private void generationUpDownValueChanged(object sender, EventArgs e)
        {
            terrainGenerationDataBindingSource.EndEdit();
        }

        private void terrainWrapperUpDownValueChanged(object sender, EventArgs e)
        {
            terrainWrapperBindingSource.EndEdit();
        }
    }
}
